<?php

declare(strict_types=1);

namespace App\Controller\Admin\System;

use App\Entity\AdminUser;
use App\Entity\Notice;
use App\Entity\NoticeType;
use App\Utils\Annotation\ApiAuthority;
use App\Utils\Annotation\ApiName;
use App\Utils\Annotation\ApiParam;
use App\Utils\Base\FilterBase;
use App\Utils\Traits\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class NoticeController
 * @package App\Controller\Admin\System
 * @Route("/admin/system/notice", name="admin-system-notice-")
 * @ApiName(name="消息通知")
 */
class NoticeController extends AbstractController {
    use ControllerTrait;

    /**
     * @ApiName(name="列表")
     * @ApiParam(name="notice_type_id", type="integer", comment="消息通知类型ID")
     * @ApiParam(name="title", type="string", comment="标题")
     * @ApiParam(name="date", type="array", comment="日期")
     * @ApiParam(name="page", type="integer", comment="页码数", default="1")
     * @ApiParam(name="limit", type="integer", comment="每页条数", default="10")
     * @ApiAuthority(need="admin.system.notice")
     * @Route("/list", methods={"GET"}, name="list")
     * @param Request $request
     */
    public function list (Request $request) {
        $params = $this->onlyDeclare($request);
        $notice_data = $this->getDoctrine()->getRepository(Notice::class)->search($params);
        $notice_total = $this->getDoctrine()->getRepository(Notice::class)->searchTotal($params);
        // 数据整合
        foreach ($notice_data as &$notice) {
            $this->getDoctrine()->getRepository(Notice::class)->buildData($notice);
        }

        return json_success([
            'data'  => $notice_data,
            'total' => $notice_total,
        ]);
    }

    /**
     * @ApiName(name="新建")
     * @ApiParam(name="notice_type_id", type="integer", comment="消息通知类型ID", validation="enum:notice_type_map", require=true)
     * @ApiParam(name="title", type="string", comment="标题", require=true)
     * @ApiParam(name="content", type="string", comment="内容", require=true)
     * @ApiParam(name="from_type", type="integer", comment="发送方类型", default="0", validation="enum:notice_from_type", example="0系统，1用户")
     * @ApiParam(name="from_id", type="integer", comment="发送方类型值", default="0", example="0表示系统，其他代表各类型对应值")
     * @ApiParam(name="to_type", type="integer", comment="接收方类型", validation="enum:notice_to_type", example="0全部，1用户组 2用户", require=true)
     * @ApiParam(name="to_id", type="array", comment="接收方类型值", require=true)
     * @ApiAuthority(need="admin.system.notice")
     * @Route("/create", methods={"POST"}, name="create")
     * @param Request $request
     */
    public function create (Request $request) {
        $params = $this->onlyDeclare($request);
        $notice = new Notice();
        $notice->setNoticeTypeId($params['notice_type_id']);
        $notice->setTitle($params['title']);
        $notice->setContent($params['content']);
        $notice->setFromType($params['from_type']);
        $notice->setFromId($params['from_id']);
        $notice->setToType($params['to_type']);
        $notice->setToId($params['to_id']);
        $notice->setAdminUserId($this->getUser()->getId());
        $this->getEntityManage()->persist($notice);
        $this->getEntityManage()->flush();
        return json_success();
    }

    /**
     * @ApiName(name="信息")
     * @ApiParam(name="id", type="integer", comment="消息通知ID", require=true)
     * @ApiAuthority(need="admin.system.notice")
     * @Route("/info", methods={"GET"}, name="info")
     * @param Request $request
     */
    public function info (Request $request) {
        $params = $this->onlyDeclare($request);
        $notice = $this->getDoctrine()->getRepository(Notice::class)->findToArray($params['id']);
        if ($notice) {
            $this->getDoctrine()->getRepository(Notice::class)->buildData($notice);
        } else {
            not_found_exception();
        }
        return json_success($notice);
    }

    /**
     * @ApiName(name="删除")
     * @ApiParam(name="id", type="integer", comment="消息通知ID", require=true)
     * @ApiAuthority(need="admin.system.notice")
     * @Route("/delete", methods={"POST"}, name="delete")
     * @param Request $request
     */
    public function delete (Request $request) {
        $params = $this->onlyDeclare($request);
        $notice = $this->getDoctrine()->getRepository(Notice::class)->findOneBy(['id' => $params['id'], 'status' => true]);
        if (!$notice) {
            not_found_exception();
        }
        $notice->setStatus(false);
        $this->getEntityManage()->flush();
        return json_success();
    }

    /**
     * @ApiName(name="获取消息类型列表")
     * @ApiAuthority(need="admin.system.notice")
     * @Route("/type-list", methods={"GET"}, name="type_list")
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function typeList () {
        $notice_types = $this->getDoctrine()->getRepository(NoticeType::class)->findAllToArrayTwo();
        return json_success(['data' => $notice_types]);
    }

    /**
     * @ApiName(name="搜索指定用户")
     * @ApiParam(name="username", type="string", comment="用户名", require=true)
     * @ApiParam(name="limit", type="integer", comment="请求结果数", default="100")
     * @ApiAuthority(need="admin.system.notice")
     * @Route("/admin-user/search", methods={"GET"}, name="admin_user-search")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Exception
     */
    public function searchAdminUser (Request $request) {
        $params = $this->onlyDeclare($request);
        $select = [
            FilterBase::ALIAS.'.id',
            FilterBase::ALIAS.'.username',
        ];
        $result = $this->getDoctrine()->getRepository(AdminUser::class)->search($params, $select);
        return json_success(['data' => $result]);
    }
}